<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');

$money = $user->data['user_money'];
$user_id 	= $user->data['user_id'];


$s_time = microtime(true);



$MA 			= array();
$CORP_MAX		= 50;
$DIFF_MIN		= 0.2;

// MA5 > MA20
$corp_MA			= array();
$corp_MA_Diff		= array();
$corp_recent_prices	= array();

$ma5_gt_ma20_number		= 1;
$query	= "select * from sv_corp_info WHERE ma5 > ma20"; 
$result	= $db->sql_query($query); 
while( $thisrow = $db->sql_fetchrow($result))
{
   	$corp_id = $thisrow['corp_id'];    
    	
   	$MA[0] = $thisrow['ma5'];				
   	$MA[1] = $thisrow['ma10'];
	$MA[2] = $thisrow['ma20'];				

	// if MA20 is near zero, skip
	if ($MA[0] < 0.01 || $MA[2] < 0.01)
		continue;
		
	// if the difference too small, skip 	
	if (abs(($MA[0] - $MA[2])/$MA[0]) < $DIFF_MIN)
		continue;
	
	
	$corp_MA[$corp_id] 				= $MA;	
	$corp_MA_Diff[$corp_id] 		= abs(($MA[0] - $MA[2])/$MA[2]);
	$corp_recent_prices[$corp_id]	= $thisrow['recent_prices'];
	
	++$ma5_gt_ma20_number;
}

arsort($corp_MA_Diff);

$i = 0;
foreach ($corp_MA_Diff as $id => $diff)
{   
	$MA = $corp_MA[$id]; 		
	$template->assign_block_vars('ma5_gt_ma20_list', array(					
			'CORP_ID'						=> $id,
			'CORP_NAME'						=> get_corp_chinese_name($id),
			'MA5'							=> round($MA[0], 1),
			'MA10'							=> round($MA[1], 1),
			'MA20'							=> round($MA[2], 1),
			'DIFF_PERCENT'					=> round($diff * 100, 1),
			'RECENT_PRICES'					=> $corp_recent_prices[$id], 
			'MAX'							=> get_max($corp_recent_prices[$id]),
			'MIN'							=> get_min($corp_recent_prices[$id]),
			)
		);
	
	++$i;
	if ($i > $CORP_MAX)
		break;
}

// MA5 < MA20
$corp_MA			= array();
$corp_MA_Diff		= array();
$corp_recent_prices	= array();

$ma5_lt_ma20_number		= 1;
$query	= "select * from sv_corp_info WHERE ma5 < ma20"; 
$result	= $db->sql_query($query); 
while( $thisrow = $db->sql_fetchrow($result))
{
   	$corp_id = $thisrow['corp_id'];    
    	
   	$MA[0] = $thisrow['ma5'];				
   	$MA[1] = $thisrow['ma10'];
	$MA[2] = $thisrow['ma20'];				
	
	// if MA20 is near zero, skip
	if ($MA[0] < 0.01 || $MA[2] < 0.01)
		continue;
		
	// if the difference too small, skip 
	if (abs(($MA[0] - $MA[2])/$MA[0]) < $DIFF_MIN)
		continue;
	
	$corp_MA[$corp_id] 				= $MA;	
	$corp_MA_Diff[$corp_id] 		= abs(($MA[0] - $MA[2])/$MA[2]);
	$corp_recent_prices[$corp_id]	= $thisrow['recent_prices'];
		
	++$ma5_lt_ma20_number;		
}

arsort($corp_MA_Diff);

$i = 0;
foreach ($corp_MA_Diff as $id => $diff)
{   
	$MA = $corp_MA[$id]; 		
	//echo "corp:$id , " . $corp_recent_prices[$id] . " <BR>\n";
	
	$template->assign_block_vars('ma5_lt_ma20_list', array(					
			'CORP_ID'						=> $id,
			'CORP_NAME'						=> get_corp_chinese_name($id),
			'MA5'							=> round($MA[0], 1),
			'MA10'							=> round($MA[1], 1),
			'MA20'							=> round($MA[2], 1),
			'DIFF_PERCENT'					=> round($diff * 100, 1),
			'RECENT_PRICES'					=> $corp_recent_prices[$id], 
			'MAX'							=> get_max($corp_recent_prices[$id]),
			'MIN'							=> get_min($corp_recent_prices[$id]),
			)
		);
	
	++$i;
	if ($i > $CORP_MAX)
		break;
}


$template->assign_vars(array(

	'MA5_GT_MA20_NUM'			=> $ma5_gt_ma20_number,
	'MA5_LT_MA20_NUM'			=> $ma5_lt_ma20_number,
	
	));

$e_time = microtime(true) - $s_time;
//echo "it takes $e_time seconds";









display_forums('', $config['load_moderators']);

// include nav links.
include_nav_links();

// Assign index specific vars
$template->assign_vars(array(

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'			=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_QUERY_USER'				=> append_sid("{$phpbb_root_path}query_user.$phpEx", 'mode=query_user'),
	'S_FULL_RANKING'			=> append_sid("{$phpbb_root_path}full_ranking.$phpEx", ''),
	'S_FULL_RANKING_1_MONTH_ROI'	=> append_sid("{$phpbb_root_path}full_ranking.$phpEx", 'mode=1_month_roi'),
	'S_FULL_RANKING_3_MONTH_ROI'	=> append_sid("{$phpbb_root_path}full_ranking.$phpEx", 'mode=3_month_roi'),
	'S_FULL_RANKING_6_MONTH_ROI'	=> append_sid("{$phpbb_root_path}full_ranking.$phpEx", 'mode=6_month_roi'),
	'S_FULL_RANKING_12_MONTH_ROI'	=> append_sid("{$phpbb_root_path}full_ranking.$phpEx", 'mode=12_month_roi'),
	
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,
	'U_MONEY'					=> number_format($money),
		
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '',
	
	'ALLOW_AD'			=> is_allow_ad($user_id),
	
	'S_INCLUDE_JQUERY'	=> true,
	)
);

// Output page
page_header($user->lang['SV_STOCK_SELECTION']);

$template->set_filenames(array(
	'body' => 'stock_selection.html')
);

page_footer();




function get_max($str)
{
	$value = explode(',', $str);
	
	$max = 0;
	
	foreach($value as $v)
	{
		if ($v > 0 && $v > $max)
			$max = $v;	
	}
		
	return $max;
}

function get_min($str)
{
	$value = explode(',', $str);
	
	$min = PHP_INT_MAX;
	
	foreach($value as $v)
	{
		if ($v > 0 && $v < $min)
			$min = $v;
	}
		
	return $min;
}
?>